package com.gidgabs.modules.api.controller;

import com.gidgabs.common.annotation.AuthIgnoreApi;
import com.gidgabs.common.utils.R;
import com.gidgabs.utils.Base64ToMultipart;
import com.gidgabs.utils.FastDFSClientWrapper;
import io.swagger.annotations.Api;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 文件上传统一接口
 */
@RestController
@RequestMapping("/api/fileUpload")
@Api(value = "文件上传接口",tags = "测试tag1")
public class ApiFileUpladController {


    @Autowired
    private FastDFSClientWrapper fastDFSClientWrapper;


    @AuthIgnoreApi
    @PostMapping(value = "/uploadimageFiles")
    public List<Map> fileUploads(MultipartFile[] upfiles) throws IOException {
        try {
            List<Map> listMap = new ArrayList<>();
            for (int i = 0; i < upfiles.length; i++) {

                String fileName = upfiles[i].getOriginalFilename();
                String address = fastDFSClientWrapper.uploadFile(upfiles[i]);
                Map<String, Object> result = new HashedMap();
                result.put("state", "SUCCESS");
                result.put("url", address);
                result.put("title", fileName);
                listMap.add(result);
            }
            return listMap;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    @AuthIgnoreApi
    @PostMapping(value = "/uploadimageFile")
    public Map fileUpload(MultipartFile upfile) throws IOException {
        try {
            String fileName = upfile.getOriginalFilename();
            String address = fastDFSClientWrapper.uploadFile(upfile);
            Map<String, Object> result = new HashedMap();
            result.put("state", "SUCCESS");
            result.put("url", address);
            result.put("title", fileName);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping(value = "/uploadimageBase64File")
    public R fileUpload1(String base64) throws IOException {
        MultipartFile file = Base64ToMultipart.base64ToMultipart(base64);
        String address = fastDFSClientWrapper.uploadFile(file);
        return R.ok().put("address",address);
    }

    @PostMapping(value = "/uploadimage3File")
    public R fileUpload3(MultipartFile file) throws IOException {
        String address = fastDFSClientWrapper.uploadFile(file);
        return R.ok().put("address", address);
    }

    @RequestMapping("/uploadimage2File")
    public Map fileUpload2(@RequestParam("upImage") File upImage) throws IOException {
        try {
            String fileName = upImage.getName();
            String address = fastDFSClientWrapper.fileUpload2(upImage);
            Map<String, Object> result = new HashedMap();
            result.put("state", "SUCCESS");
            result.put("url", address);
            result.put("title", fileName);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    //图片转化成base64字符串
    public static String GetImageStr(){
        //将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        String imgFile = "C:\\Users\\Administrator\\Desktop\\QQ截图20180607101819.png";//待处理的图片
        InputStream in = null;
        byte[] data = null;
        //读取图片字节数组
        try
        {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        //对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);//返回Base64编码过的字节数组字符串
    }


    @RequestMapping("/deleteFile")
    public R deletFile(String fileAddress) {
        fastDFSClientWrapper.deleteFile(fileAddress);
        return R.ok();
    }

    public static void main(String[] args) {
        String ss = GetImageStr();
        System.out.println(ss);

    }
}
